package com.googlecode.bipol.war.services;

import com.googlecode.bip.dao.DAOFactory;
import com.googlecode.bip.entity.IQuestion;
import com.googlecode.bip.entity.ITopic;

public class ServicesHelper {
	
	/**
	 * Return a topic referenced by it's id and local
	 * Throws an exception if topics does not exists
	 * @param idtopic The id of the topic
	 * @param idlocal Code id for localization
	 * @return The existing topic
	 * @throws ServiceException
	 */
	static ITopic getExistingTopic(int idtopic, String idlocal) throws ServiceException {
		ITopic t;
    	// Check if topic exists
    	try {
    		t = DAOFactory.getDAOFactory().getTopicDAO().findTopic(idtopic,idlocal);
    	} catch(Exception e) {
            throw new ServiceException("Error when getting topic with id \'"+idtopic+"\' and local \'"+idlocal+"\': "+e.getMessage());
        }
    	if (t==null) {
    		throw new ServiceException("Error: topic with id \'"+idtopic+"\' and local \'"+idlocal+"\' does not exists !");
    	}
    	return t;
	}
	
	/**
	 * Return a question referenced by it's id and local
	 * Throws an exception if question does not exists
	 * @param idquestion The id of the question
	 * @param idlocal Code id for localization
	 * @return The existing question
	 * @throws ServiceException
	 */
	static IQuestion getExistingQuestion(int idquestion, String idlocal) throws ServiceException {
		IQuestion q;
    	// Check if topic exists
    	try {
    		q = DAOFactory.getDAOFactory().getQuestionDAO().findQuestion(idquestion,idlocal);
    	} catch(Exception e) {
            throw new ServiceException("Error when getting question with id \'"+idquestion+"\' and local \'"+idlocal+"\': "+e.getMessage());
        }
    	if (q==null) {
    		throw new ServiceException("Error: question with id \'"+idquestion+"\' and local \'"+idlocal+"\' does not exists !");
    	}
    	return q;
	}
}
